package com.sanhe.roller.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

import com.sanhe.roller.App;
import com.sanhe.roller.R;

public class MyTextView extends AppCompatTextView {

    public MyTextView(Context context) {
        super(context);
        init(null);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            final TypedArray tArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView);
            int defFont = 0;
            int font = tArray.getInteger(R.styleable.MyTextView_harmony_font, defFont);
            Typeface typeface = null;
            switch (font){
                case 0:
                    typeface = Typeface.createFromAsset(App.getApp().getAssets(), "HarmonyOS_Sans_SC_Regular.ttf");
                    break;
                case 1:
                    typeface = Typeface.createFromAsset(App.getApp().getAssets(), "HarmonyOS_Sans_SC_Light.ttf");
                    break;
                case 2:
                    typeface = Typeface.createFromAsset(App.getApp().getAssets(), "HarmonyOS_Sans_Medium.ttf");
                    break;
                case 3:
                    typeface = Typeface.createFromAsset(App.getApp().getAssets(), "HarmonyOS_Sans_Black.ttf");
                    break;
                case 4:
                    typeface = Typeface.createFromAsset(App.getApp().getAssets(), "HarmonyOS_Sans_Black_Italic.ttf");
                    break;
                default:
                    break;
            }
            if (typeface != null){
                setTypeface(typeface);
            }

        }


    }
}
